<?php

// ... Initialize directories, load Smarty, connect to database, etc.

// If the PEAR root directory isn't automatically checked in the include_path,
// point this script directly to the location of the PEAR calendar directory
if ( !@include 'Calendar/Calendar.php' ) { 
	define('CALENDAR_ROOT','/usr/share/pear/Calendar/'); 
} 
// load these components of Pear Calendar
require_once CALENDAR_ROOT.'Month.php';
require_once CALENDAR_ROOT.'Day.php'; 
require_once CALENDAR_ROOT.'Month/Weekdays.php';
require_once CALENDAR_ROOT.'Decorator.php';
require_once CALENDAR_ROOT.'Decorator/Textual.php';
require_once CALENDAR_ROOT.'Decorator/Uri.php';
require_once CALENDAR_ROOT.'Hour.php';
require_once CALENDAR_ROOT.'Minute.php';

//var_dump('helping');

		// Decorator to "attach" functionality to selected hours
	class DiaryEvent extends Calendar_Decorator {
		var $entry;
		function DiaryEvent($calendar) {
			Calendar_Decorator::Calendar_Decorator($calendar);
		}
		function setEntry($entry) {
			$this->entry = $entry;
		}
		function getEntry() {
			return $this->entry;
		}
	}//end diaryevent
			
class DiaryEvents extends Calendar_Decorator { 
    var $entries = array();
     
    function addEntry($entry) { 
        $this->entries[] = $entry; 
    } 

    function getEntry() { 
        $entry = each($this->entries); 
        if ($entry) { 
            return $entry['value']; 
        } else { 
            reset($this->entries); 
            return false; 
        } 
    }
    function entryCount() {
        return count($this->entries);
    }
} 

class MonthPayload_Decorator2 extends Calendar_Decorator {
    function build($events=array()) { 
		//var_dump('MPD2');
// comment the following line out if you want to call "$month->build($selectedDays);" below!! Otherwise you'll get strange results!
        parent::build(); 
        foreach ($this->calendar->children as $i=> $child) { 
            $this->calendar->children[$i] = &new DiaryEvents($this->calendar->children[$i]);
        }
		//var_dump($events);
		//var_dump('count');
        if (count($events) > 0) { 
            $this->setSelection($events); 
        }
        return true; 
    }
    function setSelection($events) {
		//var_dump('set Selection');
        foreach ($this->calendar->children as $i=> $child) { 
            $stamp1 = $this->calendar->cE->dateToStamp(
                $child->thisYear(), $child->thisMonth(), $child->thisDay());
            $stamp2 = $this->calendar->cE->dateToStamp(
                $child->thisYear(), $child->thisMonth(), $child->nextDay());
            foreach ($events as $event) {
				$start = $this->time_converter($event, 'start');
				$end = $this->time_converter($event, 'end');
                if (//($stamp1 >= $start && $stamp1 < $end) ||
                    //($stamp2 >= $start && $stamp2 < $end) ||
                    ($stamp1 <= $start && $stamp2 > $end)
                /*if (($stamp1 >= $event['start'] && $stamp1 < $event['end']) || 
                    ($stamp1 <= $event['start'] && $stamp2 > $event['end'])*/ ) {
                        $this->calendar->children[$i]->addEntry($event);
                        $this->calendar->children[$i]->setSelected();
                }//end if
            }//end foreach
        }//end foreach
    }//end setSelection mehtod
	
	function time_converter($event, $task){
		switch($task){
			case 'start':
				$start = $event->start_date.' '.$event->start_time;
				list($date, $time) = explode(' ', $start);
 	 		    list($year, $month, $day) = explode('-', $date);
   				list($hour, $minute, $second) = explode(':', $time);
    		    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
    		    return $timestamp;
				break;
				
			case 'end':
				$start = $event->start_date.' '.$event->start_time;
				list($date, $time) = explode(' ', $start);
 	 		    list($year, $month, $day) = explode('-', $date);
   				list($hour, $minute, $second) = explode(':', $time);
    		    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
    		    return $timestamp + 1;
				//return $end;				
				break;
		
		}
	}
}//edn class

?>